package com.itheima.service.impl;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    private static final String PASS = "PASS";

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SetmealDao setmealDao;


    @Override
    public Result order(Map<String, Object> map) {
        String message = "";
        try {
            Date orderDate = DateUtils.parseString2Date(map.get("orderDate").toString());
            OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(orderDate);
            message = orderSetting == null?
                    MessageConstant.SELECTED_DATE_CANNOT_ORDER:
                    orderSetting.getReservations() < orderSetting.getNumber()?PASS:MessageConstant.ORDER_FULL;
            if (message.equals(PASS)) {
                Member member = memberDao.findByTelephone(map.get("telephone").toString());
                if (member == null) {
                    member = new Member();
                    member.setName(map.get("name").toString());
                    member.setIdCard(map.get("idCard").toString());
                    member.setPhoneNumber(map.get("telephone").toString());
                    member.setSex(map.get("sex").toString());
                    memberDao.add(member);
                }

                Order order = new Order();
                order.setMemberId(member.getId());
                order.setOrderDate(orderDate);
                order.setSetmealId(Integer.parseInt(map.get("setmealId").toString()));

                if (orderDao.findOrederByCondition(order) != null) {
                    message = MessageConstant.HAS_ORDERED;
                } else {
                    //TODO 预约类型此次需要调整  微信和电话   此次可以用Map.put传递设置
                    order.setOrderType(Order.ORDERTYPE_WEIXIN);
                    order.setOrderStatus(Order.ORDERSTATUS_NO);
                    orderDao.add(order);

                    orderSetting.setReservations(orderSetting.getReservations()+1);
                    orderSettingDao.editReservationsByOrderDate(orderSetting);

                    message = MessageConstant.ORDER_SUCCESS;

                    return new Result(true,message,order.getId());

                    //预约成功
                }
            }
        } catch (Exception e) {
            if(e instanceof ParseException){
                message = "时间转换异常";
            }else{
                message = "预约失败";
            }
            e.printStackTrace();
        }
        return new Result(false,message);
    }

    @Override
    public Result findById(Integer id) {

        Order order = orderDao.findById(id);

        Map<String,Object> map = new HashMap<>();
        map.put("member",memberDao.findById(order.getMemberId()).getName());
        map.put("setmeal",setmealDao.findById(order.getSetmealId()).getName());
        try {
            map.put("orderDate",DateUtils.parseDate2String(order.getOrderDate()));
        } catch (Exception e) {
            map.put("orderDate",order.getOrderDate());
        }
        map.put("orderType",order.getOrderType());

        return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
    }
}
